﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace taskmis
{
    public partial class FrmTask : Form
    {
        SqlConnection scKengee_IT_MIS;
        ClassTask ctTask;
        ClassCommon.FormOperateState ccfosCurrentState;

        public FrmTask(SqlConnection scn,ClassCommon.FormOperateState ccfos)
        {
            InitializeComponent();
            scKengee_IT_MIS = scn;
            ccfosCurrentState = ccfos;

            dgvShop.Columns.Add("ID", "ID");
            dgvShop.Columns.Add("ShopNo", "门店编号");
            dgvShop.Columns.Add("ShopName", "店名");
            dgvShop.Columns.Add("ShopState", "门店状态");
            dgvShop.Columns.Add("ShopLever", "等级");
            dgvShop.Columns.Add("Telphone", "座机电话");
            dgvShop.Columns.Add("ManagerPhone", "店长电话");
            dgvShop.Columns.Add("ForcePhone", "裱花电话");
            dgvShop.Columns.Add("CircuitTypes", "线路类型");
            dgvShop.Columns.Add("SpecialGateway", "特殊网关");
            dgvShop.Columns.Add("CircuitNumber", "宽带号/光纤号");
            dgvShop.Columns.Add("PartnerNumber", "商户号");
            dgvShop.Columns.Add("TerminalNumber", "终端号");
            dgvShop.Columns.Add("Port", "端口");
            dgvShop.Columns.Add("FastCode", "快速代码");            
            dgvShopColumnsHide("ID,门店状态,店长电话,裱花电话,宽带号/光纤号,商户号,终端号,端口");
        }

        private void SetdgvShopColumnsDataType()
        {
        }

        private void dgvShopColumnsHide(string DisplayColumnName)
        {
            for (int i = 0; i < dgvShop.ColumnCount; i++)
            {
                if (DisplayColumnName.Contains(dgvShop.Columns[i].HeaderText))
                {
                    dgvShop.Columns[i].Visible = false;
                }
                else
                {
                    dgvShop.Columns[i].Visible = true;
                }
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            string sTaskType = (sender as RadioButton).Text;
            GetFastInput(sTaskType);
            gbTaskType.Text = "任务分类-" + sTaskType;
        }

        private void SetTaskType(string sTaskType)
        //设置任务类型
        {
            for (int i = 0; i < gbTaskType.Controls.Count; i++)
            {
                string sCurrentRadioButton = (gbTaskType.Controls[i] as RadioButton).Text;
                if (sTaskType.Equals(sCurrentRadioButton))
                {
                    (gbTaskType.Controls[i] as RadioButton).Checked = true;
                }
            }
        }

        #region 快速录入
        private void GetFastInput(string TaskType)
        //得到快速录入的数据
        {
            string sqlFitment = @"select InputContent from FastInput where (TaskType='" + TaskType + "')and(InputObject='对象')";
            SqlDataAdapter sdaFitment = new SqlDataAdapter(sqlFitment, scKengee_IT_MIS);
            DataTable dtFitment = new DataTable();
            sdaFitment.Fill(dtFitment);
            sdaFitment.Dispose();
            SetFastInput(dtFitment, lvFitment);

            string sqlDescribe = @"select InputContent from FastInput where (TaskType='" + TaskType + "')and(InputObject='描述')";
            SqlDataAdapter sdaDescribe = new SqlDataAdapter(sqlDescribe, scKengee_IT_MIS);
            DataTable dtDescribe = new DataTable();
            sdaDescribe.Fill(dtDescribe);
            sdaDescribe.Dispose();
            SetFastInput(dtDescribe, lvDescribe);
        }

        private void SetFastInput(DataTable dtFastInput,ListView lvFastInput)
        //设置ListView中的快速录入数据
        {
            lvFastInput.Items.Clear();
            for (int i = 0; i < dtFastInput.Rows.Count; i++)
            {
                string sFastInput = dtFastInput.Rows[i][0].ToString();
                lvFastInput.Items.Add(sFastInput);
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (lvFitment.SelectedItems.Count > 0)
            {
                rbTaskTitle.Text += lvFitment.SelectedItems[0].Text;
            }
        }

        private void listView2_DoubleClick(object sender, EventArgs e)
        {
            if (lvDescribe.SelectedItems.Count > 0)
            {
                rbTaskTitle.Text += lvDescribe.SelectedItems[0].Text + "；\r\n";
            }
        }
        #endregion 

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            switch (ccfosCurrentState)
            {
                case ClassCommon.FormOperateState.osNew:{} ; break;
                case ClassCommon.FormOperateState.osModify: { }; break;
                case ClassCommon.FormOperateState.osView: { }; break;
            }
        }

        private void SetClassTask()
        {
            //ctCurrentTask = new ClassTask();            
        }

        private void GetShopFastCode()
        //获取门店快速代码
        {            
            if (tbFastCode.Text.Trim().Length < 1)
            {
                FrmTaskObjectChoice ftoc = new FrmTaskObjectChoice(scKengee_IT_MIS);
                ftoc.ShowDialog();
                if (ftoc.DialogResult == DialogResult.OK)
                {
                    tbFastCode.Text = ftoc.Text;
                }
                ftoc.Dispose();
            } 
        }

        private bool ExistdgvShop(string FastCode)
        {
            bool result = false;
            for (int i = 0; i < dgvShop.RowCount; i++)
            {
                string FastCodeCurrentRow=dgvShop.Rows[i].Cells["FastCode"].ToString();
                if(FastCode.Equals(FastCodeCurrentRow))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        private void GetShop2dgv_FastCode(string FastCode)
        {
            if ((FastCode.Length > 0) && (ExistdgvShop(FastCode)==false))
            {

                ClassShop cs = new ClassShop(scKengee_IT_MIS);
                if (cs.GetShop_FastCode(FastCode))
                {
                    int NewRowIndex = dgvShop.Rows.Add();
                    dgvShop.Rows[NewRowIndex].Cells["ID"].Value = cs.ID;
                    dgvShop.Rows[NewRowIndex].Cells["ShopNo"].Value = cs.ShopNo;
                    dgvShop.Rows[NewRowIndex].Cells["ShopName"].Value = cs.ShopName;
                    dgvShop.Rows[NewRowIndex].Cells["ShopState"].Value = cs.ShopState;
                    dgvShop.Rows[NewRowIndex].Cells["ShopLever"].Value = cs.ShopLever;
                    dgvShop.Rows[NewRowIndex].Cells["Telphone"].Value = cs.Telphone;
                    dgvShop.Rows[NewRowIndex].Cells["ManagerPhone"].Value = cs.ManagerPhone;
                    dgvShop.Rows[NewRowIndex].Cells["ForcePhone"].Value = cs.ForcePhone;
                    dgvShop.Rows[NewRowIndex].Cells["CircuitTypes"].Value = cs.CircuitTypes;
                    dgvShop.Rows[NewRowIndex].Cells["SpecialGateway"].Value = cs.SpecialGateway;
                    dgvShop.Rows[NewRowIndex].Cells["CircuitNumber"].Value = cs.CircuitNumber;
                    dgvShop.Rows[NewRowIndex].Cells["PartnerNumber"].Value = cs.PartnerNumber;
                    dgvShop.Rows[NewRowIndex].Cells["TerminalNumber"].Value = cs.TerminalNumber;
                    dgvShop.Rows[NewRowIndex].Cells["Port"].Value = cs.ID;
                    dgvShop.Rows[NewRowIndex].Cells["FastCode"].Value = cs.ID;
                    tbFastCode.Text = string.Empty;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            GetShopFastCode();
            GetShop2dgv_FastCode(tbFastCode.Text.Trim());
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if ((sender as CheckBox).Checked)
            {
                dgvShopColumnsHide(string.Empty);
            }
            else
            {
                dgvShopColumnsHide("ID,门店状态,店长电话,裱花电话,宽带号/光纤号,商户号,终端号,端口");
            }
        }

        private void dgvShop_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string sFastCode = dgvShop.CurrentRow.Cells["FastCode"].ToString();
            ClassShop csShop = new ClassShop(scKengee_IT_MIS);
            csShop.GetShop_FastCode(sFastCode);
            FrmShop fsi = new FrmShop(csShop, ClassCommon.FormOperateState.osView);
            fsi.ShowDialog();
            fsi.Dispose();
        }
    }
}
